package com.project.petLossSystem.service.impl;

import com.project.petLossSystem.common.Constants;
import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.mapper.PetCategoryMapper;
import com.project.petLossSystem.po.AddressCategory;
import com.project.petLossSystem.po.PetCategory;
import com.project.petLossSystem.service.PetCategoryService;
import com.project.petLossSystem.util.MyBeanUtil;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.PageResult;
import com.project.petLossSystem.vo.petCategory.FirstPetCategoryVO;
import com.project.petLossSystem.vo.petCategory.SecondPetCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class PetCategoryServiceImpl implements PetCategoryService {

    @Autowired
    private PetCategoryMapper petCategoryMapper;
    @Override
    public PageResult getCategoryPage(PageQueryUtil pageQueryUtil) {
        List<PetCategory> petCategories = petCategoryMapper.petCategoryList(pageQueryUtil);
        long total = petCategoryMapper.categoryCount(pageQueryUtil);
        return new PageResult(total, pageQueryUtil.getRows(), pageQueryUtil.getPage(), petCategories);
    }

    @Override
    public String insertCategory(PetCategory petCategory) {
        // 判断父分类是否被删除
        if(petCategory.getParentId() != 0){
            PetCategory parentCategory = petCategoryMapper.getCategoryById(petCategory.getParentId());
            if(parentCategory.getIsDeleted() == 1){
                return ServiceResultEnum.PARENT_CATEGORY_DELETED.getResult();
            }
        }
        // 判断是否已存在同级的同名分类
        PetCategory tempCategory = petCategoryMapper.getCategoryByParentIdAndName(petCategory.getParentId(), petCategory.getCategoryName());
        if(tempCategory!=null){
            return ServiceResultEnum.SAME_CATEGORY_EXIST.getResult();
        }
        if(petCategoryMapper.insertCategory(petCategory) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }else{
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public String updateCategory(PetCategory petCategory) {
        // 先判断这个分类是否存在
        PetCategory category = petCategoryMapper.getCategoryById(petCategory.getCategoryId());
        if(category == null){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        // 判断名称是否与其它重复，并且不是同一个分类
        PetCategory existCategory = petCategoryMapper.getCategoryByParentIdAndName(petCategory.getParentId(), petCategory.getCategoryName());
        if(existCategory != null && !existCategory.getCategoryId().equals(petCategory.getCategoryId())){
            return ServiceResultEnum.SAME_CATEGORY_EXIST.getResult();
        }
        // 更新
        if(petCategoryMapper.updateCategory(petCategory) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }else return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public PetCategory getCategoryById(Long categoryId) {
        return petCategoryMapper.getCategoryById(categoryId);
    }

    @Override
    public List<Long> getIdListByParentIds(List<Long> categoryIds) {
        return petCategoryMapper.getIdListByParentIds(categoryIds);
    }

    @Override
    public boolean updateDeleted(List<Long> categoryIds) {
        return petCategoryMapper.updateDeletedByIds(categoryIds) > 0;
    }

    @Override
    public List<FirstPetCategoryVO> getCategoriesForIndex() {
        // 获取一级分类
        List<PetCategory> Categories = petCategoryMapper.getCategoriesByParentId((long) Constants.TOP_PARENT_ID);
        if(CollectionUtils.isEmpty(Categories)){
            return null;
        }
        List<FirstPetCategoryVO> firstCategories = MyBeanUtil.copyList(Categories, FirstPetCategoryVO.class); // 一级分类转换
        // 取第一个分类获取子分类
        FirstPetCategoryVO firstCategory = firstCategories.get(0); // 取对象
        // 二级分类
        Categories = petCategoryMapper.getCategoriesByParentId(firstCategory.getCategoryId());
        if(!CollectionUtils.isEmpty(Categories)){
            List<SecondPetCategoryVO> firstCategoryChildren = MyBeanUtil.copyList(Categories, SecondPetCategoryVO.class); // 取list和转换一起
            firstCategory.setSecondPetCategories(firstCategoryChildren); // 存入
        }
        return firstCategories;
    }

    @Override
    public List<SecondPetCategoryVO> getSecondCategories(Long categoryId) {
        List<PetCategory> petCategories = petCategoryMapper.getCategoriesByParentId(categoryId);
        if(CollectionUtils.isEmpty(petCategories)){
            return null;
        }
        return MyBeanUtil.copyList(petCategories, SecondPetCategoryVO.class);
    }
}
